package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Organization;
import com.ericom.model.ShippingMethod;
import com.ericom.service.GenericManager;

public class OrganizationAction extends BaseAction {
	private GenericManager<Organization, Long> organizationManager;
	private GenericManager<ShippingMethod, Long> shippingMethodManager;

	private Organization organization;
	private Long id;
	
	public void setShippingMethodManager(
			GenericManager<ShippingMethod, Long> shippingMethodManager) {
		this.shippingMethodManager = shippingMethodManager;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public Long getId() {
		return id;
	}

	public void setOrganizationManager(
			GenericManager<Organization, Long> organizationManager) {
		this.organizationManager = organizationManager;
	}

	private List organizations;

	public List getOrganizations() {
		return organizations;
	}

	public String list() {
		organizations = organizationManager.getAll();
		return this.SUCCESS;
	}
	public String delete(){
		organizationManager.remove(organization.getId());
		saveMessage(getText("organization.deleted"));
		return SUCCESS;
	}
	public String edit(){
		if(null!=id){
			organization=organizationManager.get(id);
		}else{
			organization=new Organization();
		}
		return SUCCESS;
	}
	public String save()throws Exception{
		if (cancel!=null){
			return CANCEL;
		}
		if (delete!=null){
			return delete();
		}
		boolean isNew=(organization.getId()==null);
		organization=organizationManager.save(organization);
		String key=(isNew)?"organization.added":"organization.updated";
		System.out.println(key);
		saveMessage(getText(key));
		if(!isNew){
			return INPUT;
		}else{
			return SUCCESS;
		}
	}
}
